package com.fick.tags;

/**
 * 显示指定channelid的栏目下的信息列表
 */
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fick.hibernate.dao.ChannelDAO;
import com.fick.hibernate.dao.NewsDAO;
import com.fick.hibernate.model.Channel;
import com.fick.hibernate.model.News;
import com.fick.util.General;
import com.fick.util.Pager;

@SuppressWarnings("serial")
public class ChannelIndexTag extends TagSupport {
	private static Log log = LogFactory.getLog(ChannelIndexTag.class);

	// 是否带栏目简介
	private boolean isDesc = true;
	// 显示几条
	private int num = 10;
	// 标题长度
	private int length = 30;
	// table属性
	private String tableAttr = "";
	// tr属性
	private String trAttr = "";
	// dot td 属性
	private String dotTdAttr = "";
	// title td 属性
	private String titleTdAttr = "";
	// date td 属性
	private String dateTdAttr = "";
	// dot 图片
	private String dotSrc = "";
	// dot 图片属性
	private String dotAttr = "";
	// <a>标签href页面
	private String linkPage = "info.jsp";
	// <a>标签属性
	private String linkAttr = "";
	// 是否显示日期
	private boolean isDate = true;
	// 是否显示new图标
	private boolean isNew = true;
	// 日期格式
	private String dateFormat = "yyyy-MM-dd";
	// 分页表格table属性
	private String pageTableAttr = "";
	// 分布表格td属性
	private String pageTdAttr = "";
	private String divAttr = "";

	public int doStartTag() throws JspException {

		StringBuffer sb = new StringBuffer("");

		HttpServletRequest request = (HttpServletRequest) pageContext
				.getRequest();
		String channelidS = request.getParameter("channelid");
		if (channelidS != null && channelidS.trim().length() > 0) {
			int channelid = Integer.parseInt(channelidS);
			int currpage = 1;
			int totalpage = 0;
			int totalcount = 0;
			int pagesize = num;
			Channel channel = new ChannelDAO().findById(channelid);
			if (channel == null) {
				doNull(sb);
			} else {
				NewsDAO dao = new NewsDAO();
				totalcount = dao.count(News.class,"where isAss='1' and channelid.channelId=" + channelid);
				if(totalcount > 0){
					List<News> list = null;
					list = dao.findPageByHql("where isAss='1' and channelid.channelId=" + channelid + " order by addDate desc",currpage, num);
				if (totalcount == 1) {
					doOneNews(sb,list.get(0));
				} else {
					// 得到总的页数
					if (totalcount % pagesize == 0) {
						totalpage = totalcount / pagesize;
					} else {
						totalpage = totalcount / pagesize + 1;
					}
					if (request.getParameter("currPage") != null) {
						currpage = Integer.parseInt(request
								.getParameter("currPage"));
						if (currpage < 1) {
							currpage = 1;
						}
						if (currpage > totalpage) {
							currpage = totalpage;
						}
					}
					sb.append("<h1>");
					sb.append(channel.getName());
					sb.append("</h1>");
					if (isDesc && channel.getChdescribe() != null && !"".equals(channel.getChdescribe())) {
						sb.append(channel.getChdescribe());
					}
					News news;
					sb.append("<div ");
					sb.append(divAttr);
					sb.append("><table ");
					sb.append(tableAttr);
					sb.append(" ><tbody>");
					for (int i = 0; i < list.size(); i++) {
						news = list.get(i);
						sb.append("<tr ");
						sb.append(trAttr);
						sb.append(" >");
						sb.append("<td ");
						sb.append(dotTdAttr);
						sb.append(" >");
						if (dotSrc.trim().length() > 0) {
							sb.append("<img src='");
							sb.append(dotSrc);
							sb.append("' ");
							sb.append(dotAttr);
							sb.append(" />");
						}
						sb.append("</td>");
						sb.append("<td ");
						sb.append(titleTdAttr);
						sb.append(" >");

						sb.append("<a href='");
						sb.append(linkPage);
						sb.append("?channelid=");
						sb.append(channelid);
						sb.append("&newsid=");
						sb.append(news.getNewId());
						sb.append("' ");
						sb.append(linkAttr);
						sb.append(" >");
						sb.append(General.trimStringInLength(news.getTitle(),
								length));

						// 是否显示new图标
						if (isNew) {
							SimpleDateFormat myFormatter = new SimpleDateFormat(
									"yyyy-MM-dd");
							long day = 0;
							try {
								java.util.Date date = myFormatter
										.parse(myFormatter.format(new Date()));
								java.util.Date mydate = myFormatter
										.parse(myFormatter.format(news
												.getAddDate()));
								day = (date.getTime() - mydate.getTime())
										/ (24 * 60 * 60 * 1000);
							} catch (Exception er) {
								er.printStackTrace();
							}
							if (day <= 0) {
								sb.append("<img src='images/new.gif'>");
							}
						}
						sb.append("</a>");
						// sb.append("&nbsp;&nbsp;&nbsp;");
						sb.append("</td>");

						if (isDate) {
							sb.append("<td ");
							sb.append(dateTdAttr);
							sb.append(" >");
							SimpleDateFormat df = new SimpleDateFormat(
									dateFormat);
							sb.append(df.format(news.getAddDate()));
							sb.append("</td>");
						}
						sb.append("</tr>");
					}
					sb.append("</tbody></table>");
					// 显示分页代码
					sb.append("<table ");
					sb.append(pageTableAttr);
					sb.append(" >");
					sb.append("<tr>");
					sb.append("<td ");
					sb.append(pageTdAttr);
					sb.append(" >");
					sb.append(Pager.nprintTable(channel.getUrl()
							+ "?channelid=" + channelid, currpage, totalpage,
							totalcount, "&", ""));
					sb.append("</td>");
					sb.append("</tr>");
					sb.append("</table></div>");
				}
			}else {
					doNull(sb);
				}
			} 
		} else {
			doNull(sb);
		}

		JspWriter out = pageContext.getOut();
		try {
			out.println(sb.toString());
			out.flush();
		} catch (IOException e) {
			log.error("标签显示错误", e);
		}
		return SKIP_BODY;
	}

	private void doOneNews(StringBuffer sb,News news) {
		sb.append("<h1>");
		sb.append(news.getTitle());
		sb.append("</h1>");
		sb.append(news.getContent());
	}

	private void doNull(StringBuffer sb) {
		sb.append("<p>");
		sb.append("Thers is not a right channelid neither a newsid!");
		sb.append("</p>");
	}

	public boolean isDesc() {
		return isDesc;
	}

	public void setDesc(boolean isDesc) {
		this.isDesc = isDesc;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public String getTableAttr() {
		return tableAttr;
	}

	public void setTableAttr(String tableAttr) {
		this.tableAttr = tableAttr;
	}

	public String getTrAttr() {
		return trAttr;
	}

	public void setTrAttr(String trAttr) {
		this.trAttr = trAttr;
	}

	public String getDotTdAttr() {
		return dotTdAttr;
	}

	public void setDotTdAttr(String dotTdAttr) {
		this.dotTdAttr = dotTdAttr;
	}

	public String getTitleTdAttr() {
		return titleTdAttr;
	}

	public void setTitleTdAttr(String titleTdAttr) {
		this.titleTdAttr = titleTdAttr;
	}

	public String getDateTdAttr() {
		return dateTdAttr;
	}

	public void setDateTdAttr(String dateTdAttr) {
		this.dateTdAttr = dateTdAttr;
	}

	public String getDotSrc() {
		return dotSrc;
	}

	public void setDotSrc(String dotSrc) {
		this.dotSrc = dotSrc;
	}

	public String getDotAttr() {
		return dotAttr;
	}

	public void setDotAttr(String dotAttr) {
		this.dotAttr = dotAttr;
	}

	public String getLinkPage() {
		return linkPage;
	}

	public void setLinkPage(String linkPage) {
		this.linkPage = linkPage;
	}

	public String getLinkAttr() {
		return linkAttr;
	}

	public void setLinkAttr(String linkAttr) {
		this.linkAttr = linkAttr;
	}

	public boolean isDate() {
		return isDate;
	}

	public void setDate(boolean isDate) {
		this.isDate = isDate;
	}

	public boolean isNew() {
		return isNew;
	}

	public void setNew(boolean isNew) {
		this.isNew = isNew;
	}

	public String getDateFormat() {
		return dateFormat;
	}

	public void setDateFormat(String dateFormat) {
		this.dateFormat = dateFormat;
	}

	public String getPageTableAttr() {
		return pageTableAttr;
	}

	public void setPageTableAttr(String pageTableAttr) {
		this.pageTableAttr = pageTableAttr;
	}

	public String getPageTdAttr() {
		return pageTdAttr;
	}

	public void setPageTdAttr(String pageTdAttr) {
		this.pageTdAttr = pageTdAttr;
	}

	public String getDivAttr() {
		return divAttr;
	}

	public void setDivAttr(String divAttr) {
		this.divAttr = divAttr;
	}

}
